package com.xiangmo.travel.Controller;

import com.xiangmo.travel.Annotation.TokenRequired;
import com.xiangmo.travel.Dto.CommunityLikeDto;
import com.xiangmo.travel.Dto.ResultData;
import com.xiangmo.travel.Enums.CodeAndMsg;
import com.xiangmo.travel.Service.CommunityLikeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/communityLike")
public class CommunityLikeController {
    @Autowired
    private CommunityLikeService communityLikeService;

    //点赞
    @TokenRequired
    @PostMapping("/add")
    public ResultData<Integer> addCommunityLike(@RequestBody CommunityLikeDto communityLikeDto, @RequestHeader("Authorization") String token){
        //添加判断，判断用户是否已经点赞过了
        if(communityLikeService.checkCommunityIsLike(communityLikeDto) == 1){
            log.warn("社区帖子：用户已经点过赞却重复请求点赞！");
            return new ResultData<>(CodeAndMsg.LIKE_ERROR.getCode(), CodeAndMsg.LIKE_ERROR.getMessage(),
                    null, token.substring(7));
        }
        int status = communityLikeService.addCommunityLike(communityLikeDto);
        if(status == 1){
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(), CodeAndMsg.SUCCESS.getMessage(),
                    null, token.substring(7));
        }
        log.warn("社区帖子：点赞异常！");
        return new ResultData<>(CodeAndMsg.ERROR.getCode(), CodeAndMsg.ERROR.getMessage(),
                null, token.substring(7));
    }

    //取消点赞
    @TokenRequired
    @DeleteMapping("/cancel")
    public ResultData<Integer> deleteCommunityLike(@RequestBody CommunityLikeDto communityLikeDto, @RequestHeader("Authorization") String token){
        int status = communityLikeService.deleteCommunityLike(communityLikeDto);
        if(status == 1){
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(), CodeAndMsg.SUCCESS.getMessage(),
                    null, token.substring(7));
        }
        log.warn("社区帖子：用户取消点赞失败！");
        return new ResultData<>(CodeAndMsg.ERROR.getCode(), CodeAndMsg.ERROR.getMessage(),
                null, token.substring(7));
    }
}
